<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-16
 * Time: 17:16
 */

namespace app\api\controller\v1;

use app\api\model\User;
use app\api\model\UserMoney;
use app\api\service\member\UpdateMember;
use app\api\validate\member\CheckGrade;

class Member
{
    /**
     * 获取用户列表
     * @validate('PagingParameter')
     */
    public function getMemberList($page,$limit,$nickname,$user_no,$phone,$grade_name)
    {
        $where[] = ['status', '=', 1];
        if($nickname) {
            $where[] = ['nickname','like','%'.$nickname.'%'];
        }
        if($user_no) {
            $where[] = ['user_no','like','%'.$user_no.'%'];
        }
        if($phone) {
            $where[] = ['phone','like','%'.$phone.'%'];
        }
        if($grade_name) {
            $where[] = ['grade_name','like','%'.$grade_name.'%'];
        }
        $query = User::where($where)->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['staff_status'] == 0) {
                $item['staff_status_name'] = '不是取送员';
            } else {
                $item['staff_status_name'] = '取送员';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }
    /**
     * 获取余额列表
     * @validate('PagingParameter')
     */
    public function getMoneyList($page,$limit,$user_id)
    {
        $query = UserMoney::where('user_id',$user_id)
            ->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['type'] == 1) {
                $item['type_name'] = '余额充值得到';
            } elseif($item['type'] == 2) {
                $item['type_name'] = '支付订单失去';
            } elseif($item['type'] == 3) {
                $item['type_name'] = '直接使用失去';
            } elseif($item['type'] == 4) {
                $item['type_name'] = '后台操作得到';
            } elseif($item['type'] == 5) {
                $item['type_name'] = '后台操作失去';
            } elseif($item['type'] == 6) {
                $item['type_name'] = '公众号余额得到';
            } elseif($item['type'] == 7) {
                $item['type_name'] = '申请退款得到';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 修改用户余额
     */
    public function updateMemberMoney($type,$money,$id)
    {
        $updateMoney = new UpdateMember($type,$money,$id);
        return $updateMoney->updateData();
    }

    /**
     * 修改用户等级名称
     */
    public function updateMemberGrade($id,$grade_name)
    {
        (new CheckGrade())->goCheck();
        User::where('id',$id)->update([
            'grade_name' => $grade_name,
        ]);
        return writeJson(201, [], '修改成功!');
    }
}